/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

using System;
using System.Collections.Generic;

namespace FaceCat {
    /// <summary>
    /// 下拉列表视图的列表
    /// </summary>
    public class FCComboBoxMenu : FCMenu {
        /// <summary>
        /// 创建列表
        /// </summary>
        public FCComboBoxMenu() {

        }

        public FCComboBox m_comboBox;

        /// <summary>
        /// 获取列表视图
        /// </summary>
        public virtual FCComboBox getComboBox() {
            return m_comboBox;
        }

        /// <summary>
        /// 设置列表视图
        /// </summary>
        public virtual void setComboBox(FCComboBox value) {
            m_comboBox = value;
        }

        /// <summary>
        /// 是否自动隐藏
        /// </summary>
        /// <returns>是否隐藏</returns>
        public override bool onAutoHide() {
            if (m_comboBox != null && m_comboBox.isFocused()) {
                return false;
            }
            return true;
        }
    }

    /// <summary>
    /// 下拉列表视图
    /// </summary>
    public class FCComboBox : FCTextBox, FCMenuItemTouchEventCallBack {
        /// <summary>
        /// 创建下拉列表
        /// </summary>
        public FCComboBox() {
        }

        public FCButton m_dropDownButton;

        /// <summary>
        /// 获取下拉按钮
        /// </summary>
        public virtual FCButton getDropDownButton() {
            return m_dropDownButton;
        }

        public FCComboBoxMenu m_dropDownMenu;

        /// <summary>
        /// 获取下拉菜单
        /// </summary>
        public virtual FCComboBoxMenu getDropDownMenu() {
            return m_dropDownMenu;
        }

        /// <summary>
        /// 获取选中的索引
        /// </summary>
        public virtual int getSelectedIndex() {
            if (m_dropDownMenu != null) {
                ArrayList<FCMenuItem> items = m_dropDownMenu.getItems();
                int itemSize = items.size();
                for (int i = 0; i < itemSize; i++) {
                    FCMenuItem item = items.get(i);
                    if (item.isChecked()) {
                        return i;
                    }
                }
            }
            return -1;
        }

        /// <summary>
        /// 设置选中的索引
        /// </summary>
        public virtual void setSelectedIndex(int value) {
            if (m_dropDownMenu != null) {
                ArrayList<FCMenuItem> items = m_dropDownMenu.getItems();
                int itemSize = items.size();
                bool changed = false;
                for (int i = 0; i < itemSize; i++) {
                    FCMenuItem item = items.get(i);
                    if (i == value) {
                        if (!item.isChecked()) {
                            // 选中并设置文字
                            item.setChecked(true);
                            changed = true;
                        }
                        setText(item.getText());
                    } else {
                        item.setChecked(false);
                    }
                }
                if (changed) {
                    onSelectedIndexChanged();
                }
            }
        }

        /// <summary>
        /// 获取选中的文字
        /// </summary>
        public virtual String getSelectedText() {
            if (m_dropDownMenu != null) {
                ArrayList<FCMenuItem> items = m_dropDownMenu.getItems();
                int itemSize = items.size();
                for (int i = 0; i < itemSize; i++) {
                    FCMenuItem item = items.get(i);
                    if (item.isChecked()) {
                        return item.getText();
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// 设置选中的文字
        /// </summary>
        public virtual void setSelectedText(String value) {
            if (m_dropDownMenu != null) {
                ArrayList<FCMenuItem> items = m_dropDownMenu.getItems();
                int itemSize = items.size();
                bool changed = false;
                for (int i = 0; i < itemSize; i++) {
                    FCMenuItem item = items.get(i);
                    if (item.getText().Equals(value)) {
                        // 选中并设置文字
                        if (!item.isChecked()) {
                            item.setChecked(true);
                            changed = true;
                        }
                        setText(item.getText());
                    } else {
                        item.setChecked(false);
                    }
                }
                if (changed) {
                    onSelectedIndexChanged();
                }
            }
        }

        /// <summary>
        /// 获取选中的值
        /// </summary>
        public virtual String getSelectedValue() {
            if (m_dropDownMenu != null) {
                ArrayList<FCMenuItem> items = m_dropDownMenu.getItems();
                int itemSize = items.size();
                for (int i = 0; i < itemSize; i++) {
                    FCMenuItem item = items.get(i);
                    if (item.isChecked()) {
                        return item.getValue();
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// 设置选中的值
        /// </summary>
        public void setSelectedValue(String value) {
            if (m_dropDownMenu != null) {
                ArrayList<FCMenuItem> items = m_dropDownMenu.getItems();
                int itemSize = items.size();
                bool changed = false;
                for (int i = 0; i < itemSize; i++) {
                    FCMenuItem item = items.get(i);
                    if (item.getValue().Equals(value)) {
                        // 选中并设置文字
                        if (!item.isChecked()) {
                            item.setChecked(true);
                            changed = true;
                        }
                        setText(item.getText());
                    } else {
                        item.setChecked(false);
                    }
                }
                if (changed) {
                    onSelectedIndexChanged();
                }
            }
        }

        /// <summary>
        /// 添加菜单项
        /// </summary>
        /// <param name="item">菜单项</param>
        public virtual void addItem(FCMenuItem item) {
            if (m_dropDownMenu != null) {
                m_dropDownMenu.addItem(item);
            }
        }

        /// <summary>
        /// 触摸事件回调
        /// </summary>
        /// <param name="eventName"></param>
        /// <param name="sender"></param>
        /// <param name="touchInfo"></param>
        /// <param name="invoke"></param>
        public override void callTouchEvent(String eventName, Object sender, FCTouchInfo touchInfo, Object invoke) {
            base.callTouchEvent(eventName, sender, touchInfo, invoke);
            if (sender == m_dropDownButton) {
                if (eventName == "ontouchdown") {
                    onDropDownOpening();
                }
            }
        }

        /// <summary>
        /// 菜单触摸事件回调
        /// </summary>
        /// <param name="eventName"></param>
        /// <param name="sender"></param>
        /// <param name="item"></param>
        /// <param name="touchInfo"></param>
        /// <param name="invoke"></param>
        public void callMenuItemTouchEvent(String eventName, Object sender, FCMenuItem item, FCTouchInfo touchInfo, Object invoke) {
            if (sender == m_dropDownMenu) {
                if (eventName == "onmenuitemclick") {
                    setText(item.getText());
                    ArrayList<FCMenuItem> items = getItems();
                    int itemSize = items.size();
                    for (int i = 0; i < itemSize; i++) {
                        if (items.get(i) == item) {
                            setSelectedIndex(i);
                            break;
                        }
                    }
                    setSelectionStart(getText().Length);
                    invalidate();
                }
            }
        }

        /// <summary>
        /// 调用键盘事件
        /// </summary>
        /// <param name="eventName"></param>
        /// <param name="sender"></param>
        /// <param name="key"></param>
        /// <param name="invoke"></param>
        public override void callKeyEvent(String eventName, Object sender, char key, Object invoke) {
            base.callKeyEvent(eventName, sender, key, invoke);
            if (key == 13) {
                onSelectedIndexChanged();
            }
        }

        /// <summary>
        /// 销毁方法
        /// </summary>
        public override void delete() {
            if (!isDeleted()) {
                if (m_dropDownButton != null) {
                    m_dropDownButton.removeEvent(this, "ontouchdown");
                }
                if (m_dropDownMenu != null) {
                    m_dropDownMenu.removeEvent(this, "onmenuitemclick");
                    m_dropDownMenu.removeEvent(this, "onkeydown");
                    getNative().removeView(m_dropDownMenu);
                    m_dropDownMenu.delete();
                    m_dropDownMenu = null;
                }
            }
            base.delete();
        }

        /// <summary>
        /// 下拉按钮的点击方法
        /// </summary>
        /// <param name="sender">发送者</param>
        /// <param name="touchInfo">触摸信息</param>
        private void dropDownButtonTouchDown(Object sender, FCTouchInfo touchInfo, Object invoke) {
            onDropDownOpening();
        }

        /// <summary>
        /// 清除所有菜单项
        /// </summary>
        public virtual void clearItems() {
            if (m_dropDownMenu != null) {
                m_dropDownMenu.clearItems();
            }
        }

        /// <summary>
        /// 获取视图类型
        /// </summary>
        /// <returns>视图类型</returns>
        public override String getViewType() {
            return "ComboBox";
        }

        /// <summary>
        /// 获取事件名称列表
        /// </summary>
        /// <returns>名称列表</returns>
        public override ArrayList<String> getEventNames() {
            ArrayList<String> eventNames = base.getEventNames();
            eventNames.AddRange(new String[] { "SelectedIndexChanged" });
            return eventNames;
        }

        /// <summary>
        /// 获取菜单项
        /// </summary>
        /// <returns>菜单项集合</returns>
        public virtual ArrayList<FCMenuItem> getItems() {
            if (m_dropDownMenu != null) {
                return m_dropDownMenu.getItems();
            }
            return null;
        }

        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">返回属性值</param>
        /// <param name="type">返回属性类型</param>
        public override void getAttribute(String name, ref String value, ref String type) {
            switch (name)
            {
                case "selectedindex":
                    type = "int";
                    value = FCTran.intToStr(getSelectedIndex());
                    break;
                case "selectedtext":
                    type = "text";
                    value = getSelectedText();
                    break;
                case "selectedvalue":
                    type = "text";
                    value = getSelectedValue();
                    break;
                default:
                    base.getAttribute(name, ref value, ref type);
                    break;
            }
        }

        /// <summary>
        /// 获取属性名称列表
        /// </summary>
        /// <returns>属性名称列表</returns>
        public override ArrayList<String> getAttributeNames() {
            ArrayList<String> attributeNames = base.getAttributeNames();
            attributeNames.AddRange(new String[] { "SelectedIndex", "SelectedText", "SelectedValue" });
            return attributeNames;
        }

        /// <summary>
        /// 插入菜单项
        /// </summary>
        /// <param name="index">索引</param>
        /// <param name="item">菜单项</param>
        public virtual void insertItem(int index, FCMenuItem item) {
            if (m_dropDownMenu != null) {
                m_dropDownMenu.insertItem(index, item);
            }
        }

        /// <summary>
        /// 菜单项的点击方法
        /// </summary>
        /// <param name="sender">发送者</param>
        /// <param name="item">菜单项</param>
        /// <param name="touchInfo">触摸信息</param>
        private void menuItemClick(Object sender, FCMenuItem item, FCTouchInfo touchInfo, Object invoke) {
            setText(item.getText());
            ArrayList<FCMenuItem> items = getItems();
            int itemSize = items.size();
            for (int i = 0; i < itemSize; i++) {
                if (items.get(i) == item) {
                    setSelectedIndex(i);
                    break;
                }
            }
            setSelectionStart(getText().Length);
            invalidate();
        }

        /// <summary>
        /// 下拉菜单的按键方法
        /// </summary>
        /// <param name="sender">发送者</param>
        /// <param name="key">按键</param>
        private void menuKeyDown(Object sender, char key, Object invoke) {
            if (key == 13) {
                onSelectedIndexChanged();
            }
        }

        /// <summary>
        /// 菜单下拉方法
        /// </summary>
        public virtual void onDropDownOpening() {
            if (m_dropDownMenu != null) {
                m_dropDownMenu.setNative(getNative());
                FCPoint nativePoint = pointToNative(new FCPoint(0, getHeight()));
                m_dropDownMenu.setLocation(nativePoint);
                m_dropDownMenu.setSize(new FCSize(getWidth(), m_dropDownMenu.getContentHeight()));
                m_dropDownMenu.setWidth(getWidth());
                m_dropDownMenu.setVisible(true);
                m_dropDownMenu.bringToFront();
                m_dropDownMenu.invalidate();
            }
        }

        /// <summary>
        /// 键盘按下方法
        /// </summary>
        /// <param name="key">按键</param>
        public override void onKeyDown(char key) {
            base.onKeyDown(key);
            FCHost host = getNative().getHost();
            if (!host.isKeyPress(0x10) && !host.isKeyPress(0x11) && !host.isKeyPress(0x12)) {
                if (getLinesCount() <= 1) {
                    if (key == 13 || key == 38 || key == 40) {
                        if (m_dropDownMenu != null) {
                            m_dropDownMenu.onKeyDown(key);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 添加视图方法
        /// </summary>
        public override void onLoad() {
            base.onLoad();
            FCHost host = getNative().getHost();
            if (m_dropDownButton == null) {
                m_dropDownButton = host.createInternalView(this, "dropdownbutton") as FCButton;
                addView(m_dropDownButton);
                m_dropDownButton.addEvent(this, "ontouchdown", null);
            }
            if (m_dropDownMenu == null) {
                m_dropDownMenu = host.createInternalView(this, "dropdownmenu") as FCComboBoxMenu;
                getNative().addView(m_dropDownMenu);
                m_dropDownMenu.setVisible(false);
                m_dropDownMenu.addEvent(this, "onmenuitemclick", null);
                m_dropDownMenu.addEvent(this, "onkeydown", null);
            } else {
                m_dropDownMenu.setNative(getNative());
            }
        }

        /// <summary>
        /// 选中索引改变方法
        /// </summary>
        public virtual void onSelectedIndexChanged() {
            callEvents("onselectedindexchanged");
        }

        /// <summary>
        /// 触摸滚轮方法
        /// </summary>
        /// <param name="touchInfo">触摸信息</param>
        public override void onTouchWheel(FCTouchInfo touchInfo) {
            base.onTouchWheel(touchInfo);
            if (getLinesCount() <= 1) {
                if (m_dropDownMenu != null) {
                    FCTouchInfo newTouchInfo = touchInfo.clone();
                    newTouchInfo.m_firstPoint = m_dropDownMenu.getTouchPoint();
                    newTouchInfo.m_secondPoint = m_dropDownMenu.getTouchPoint();
                    m_dropDownMenu.onTouchWheel(newTouchInfo);
                }
            }
        }

        /// <summary>
        /// 移除菜单项
        /// </summary>
        /// <param name="item">菜单项</param>
        public virtual void removeItem(FCMenuItem item) {
            if (m_dropDownMenu != null) {
                m_dropDownMenu.removeItem(item);
            }
        }

        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">属性值</param>
        public override void setAttribute(String name, String value)
        {
            switch (name)
            {
                case "selectedindex":
                    setSelectedIndex(FCTran.strToInt(value));
                    break;
                case "selectedtext":
                    setSelectedText(value);
                    break;
                case "selectedvalue":
                    setSelectedValue(value);
                    break;
                default:
                    base.setAttribute(name, value);
                    break;
            }
        }

        /// <summary>
        /// 更新布局方法
        /// </summary>
        public override void update() {
            if (m_native != null && !m_native.m_lockUpdate)
            {
                base.update();
                int width = getWidth(), height = getHeight();
                if (m_dropDownButton != null)
                {
                    int dWidth = m_dropDownButton.getWidth();
                    m_dropDownButton.setLocation(new FCPoint(width - dWidth, 0));
                    m_dropDownButton.setSize(new FCSize(dWidth, height));
                    setPadding(new FCPadding(0, 0, dWidth, 0));
                }
            }
        }
    }
}
